package com.chilly.other.jdk8.Interface;

import org.junit.Test;

//定义函数式接口
@FunctionalInterface
interface UserBuilder {
    User builder(String name);
}

//定义含待引用的构造方法类
class User {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

/**
 * @project: jdk-learnig
 * @description: 类的构造器引用
 * @author: 黄宇辉
 * @date: 8/18/2019-7:09 PM
 * @version: 1.0
 * @website: https://yubuntu0109.github.io/
 */
public class RefConstructor {

    public void printResult(String name, UserBuilder userBuilder) {
        User user = userBuilder.builder(name);
        System.out.println(user.getName());
    }

    //传统写法
    @Test
    public void testPrintResult() {
        printResult("yubuntu0109", new UserBuilder() {
            @Override
            public User builder(String name) {
                return new User(name);
            }
        });
    }

    //Lambda表达式写法
    @Test
    public void testPrintResultWithLambda() {
        printResult("yubuntu0109", s -> new User(s));
    }

    //方法引用写法
    @Test
    public void testPrintResultWithMethodRef() {
        printResult("yubuntu0109", User::new);
    }

}